#!/usr/bin/python -S
"""
p4_test.py: Tests for p4 JSON Patterns
"""

__author__ = 'Andy Chu'


import os
from pprint import pprint
import sys

from pan.core import cmdapp
from pan.core import params
from pan.core import json
from pan.core import util
from pan.test import testy

if __name__ == '__main__':
  # for jsonpattern package
  sys.path.insert(
      0, os.path.join(os.path.dirname(__file__), '..', 'python'))
  # Root of the project
  sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from tests import test_main

import jsonpattern


class HttpHeadersTest(testy.Test):

  LABELS = ['multilanguage']

  def setUpOnce(self):
    self.y = test_main.TestData("yahoo-head.txt")
    self.g = test_main.TestData("google-head.txt")
    self.b = test_main.TestData("bing-head.txt")

  def testHttpHeaders(self):

    library = {}

    def Add(name, pattern, desc=None):
      library[name] = jsonpattern.Pattern(
          pattern, patterns=library, name=name, desc=desc)

    p = r"""
         | value
        #  Allow - or space separators, for cookies and Date: header
       SP|   { \w+ dow}, {:int day}[- ]{ \w+ month}[- ]{:int year} {}
       SP|   {:H:M:S time} { \w+ timezone}
         | end
        """
    Add('date', p)

    p = r"""
         | repeated
         |   choice
        P|     expires={:date expires}
         |   or
        P|     domain={ [^;\n]+ domain}
         |   or
        P|     path={ [^;\n]+ path }
         |   or
        P|     { [^;\n]+ value}
         |   end
        P|   (; )?
         | end
        """
    Add('set-cookie', p)

    p = r"""
       SP| HTTP/{ \d\.\d http-version } { .* reason }\n
         | repeated headers
         |   choice
       SP|     Date: {:date date}
         |   or
       SP|     Cache-Control: { .* cache-control }
         |   or
       SP|     Expires: { .* expires }
         |   or
       SP|     P3P: { .* p3p }
         |   or
       SP|     Content-Type: { [^;]* content-type }
         |     optional charset
       SP|       ; charset={ .* }
         |     end
         |   or
       SP|     Set-Cookie: {:set-cookie set-cookie}
         |   or
       SP|     Server: { .* server }
         |   or
       SP|     Transfer-Encoding: { .* transfer-encoding }
         |   or
       SP|     Vary: { .* vary }
         |   or
       SP|     X-XRDS-Location: { .* x-xrds }
         |   or
       SP|     Last-Modified: { .* last-modified }
         |   or
       SP|     Accept-Ranges: { .* accept-ranges }
         |   or
       SP|     Connection: { .* connection }
         |   or
       SP|     Content-Length: {:int content-length}
         |   end
        P|   \n
         | end
        """

    Add('head', p)

    head = library['head']

    #print head.AsTree()

    result = head.match(self.y)
    pprint( result.data)
    #print 'yahoo', id(result.failures)
    ##print
    #print '-'* 100

    print '-'* 100
    result = head.match(self.g)
    #print result.MatchLog()
    pprint(result.data)

    print '-'* 100
    result = head.match(self.b)
    #pprint (head.match(self.b).data)


    print '-'* 100
    p = r"""
       SP| HTTP/{ \d\.\d http-version } { .* reason }\n
         | repeated headers
       SP|   { [\w\-]+ name}: { .* value}\n
         | end
       """

    Add('head2', p)

    head = library['head2']

    result = head.match(self.y)
    pprint( result.data)

    print '-'* 100
    result = head.match(self.g)
    #print result.MatchLog()
    pprint(result.data)

    print '-'* 100
    result = head.match(self.b)
    pprint (head.match(self.b).data)


if __name__ == '__main__':
  test_main.main(__import__(__name__), sys.argv[1:])
